package com.fps.web.dao.impl;

import org.springframework.stereotype.Repository;

import com.fpswork.core.exception.exception.BusinessException;
import com.fpswork.core.model.PageEntity;
import com.fpswork.datamodel.basedao.BaseDao;
import com.fpswork.util.BeanUtils;
import com.fpswork.util.StringUtil;
import com.fps.web.dao.BaseUserDao;
import com.fps.web.model.BaseUser;
@Repository 
public class BaseUserDaoImpl extends BaseDao implements BaseUserDao{
    
    
	public BaseUser getUserByLoginName(String loginName) throws BusinessException{
    	String sql="select * from BaseUser  where username='"+loginName+"' ";
		return (BaseUser) this.findBySql(sql,BaseUser.class).get(0);
	}
	public PageEntity queryUserList(PageEntity pageEntity)  throws BusinessException{
		String sql="select * from BaseUser";
		pageEntity.setSql(sql);
		return  this.queryForPageEntity(pageEntity);
	}
	public BaseUser queryUserByID(String id)  throws BusinessException{
		return  this.get(BaseUser.class, id);
	}
	public BaseUser addUser(BaseUser baseUser)  throws BusinessException{
		BaseUser baseUser1= this.get(BaseUser.class, baseUser.getId());
		if(StringUtil.isEmpty(baseUser.getId())){
			this.save(baseUser);
		}else{
			if(baseUser1.getPassword().equals(baseUser.getPassword())){
				BeanUtils.copyProperties(baseUser1, baseUser);
			}else{
				BeanUtils.copyProperties(baseUser1, baseUser);
				baseUser1.setPassword(StringUtil.toMD5(baseUser.getPassword()));
			}
			this.update(baseUser1);
		}
		return baseUser1;
	}
	public Boolean deleteUser(BaseUser baseUser)  throws BusinessException{
		this.delete(baseUser);
		return true;
	}
}
